package com.bocai.dahuatech.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.NewProductBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.adapter.FlowAdapter;
import com.bocai.dahuatech.ui.adapter.HomeNewProductAdapter;
import com.bocai.dahuatech.utils.AESUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 新品速递列表
 */
public class NewProductListActivity extends BaseActivity {
    @Bind(R.id.swipe_product_third)
    SwipeRefreshLayout swipeProduct;
    @Bind(R.id.lv_product_third)
    ListView lvProductThird;
    NewProductBean bean;
    FlowAdapter adapter;
    HomeNewProductAdapter productAdapter;
    int pager=1;
    List<NewProductBean.ContentEntity> datas=new ArrayList<>();
    @Override
    protected int contentView() {
        return R.layout.activity_new_product;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.newproduct);
        getDatas();
        initEvent();
    }

    /**
     * 捕捉back
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            startActivity(new Intent(NewProductListActivity.this,HomeActivity.class));
            finish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(NewProductListActivity.this,HomeActivity.class));
                finish();
            }
        });
        swipeProduct.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pager = 1;
                getDatas();
            }
        });
    }

    @Override
    public void onEventMainThread(MainEvent event)
    {
        super.onEventMainThread(event);
        if(event.msg.equals("goshopcart"))
        {
            finish();
        }
    }
    private void getDatas() {
        showLoading();
        getTestApi().getNewProduct()
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            swipeProduct.setRefreshing(false);
            Log.e("error",e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean) {
            hideLoading();
            swipeProduct.setRefreshing(false);
            if(baseBean.getReturnNo().equals("1")) {
                String result = AESUtils.decode(baseBean.getContent());
                result = start + result + end;
                Log.e("result", result);

                bean = new Gson().fromJson(result, NewProductBean.class);
                datas.addAll(bean.getContent());
                if(pager==1){

                    productAdapter = new HomeNewProductAdapter(NewProductListActivity.this, bean);
                    lvProductThird.setAdapter(productAdapter);
                }
                lvProductThird.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(NewProductListActivity.this, NewProductDetailActivity.class);
                        intent.putExtra("productId", datas.get(position).getFPROD_ID());
                        intent.putExtra("solutionId", "0");
                        startActivity(intent);
                    }
                });

            }else{
                showToast(baseBean.getReturnInfo());
            }

        }
    };

}
